package com.shen.nettysocketchat

import com.shen.library_nettysocket.protobuf.MessageProtobuf
import com.shen.nettysocketchat.bean.AppMessage
import com.shen.nettysocketchat.bean.BaseMessage
import com.shen.nettysocketchat.bean.ContentMessage
import com.shen.nettysocketchat.bean.Head

/**
 * 消息转换
 */
object MessageBuilder {
    /**
     * 根据聊天消息，生成一条可以能够传输通讯的消息
     *
     * @param msgId
     * @param type
     * @param subType
     * @param fromId
     * @param toId
     * @param extend
     * @param content
     * @return
     */
    fun buildAppMessage(
        msgId: String?, type: Int, subType: Int, fromId: String?,
        toId: String?, extend: String?, content: String?
    ): AppMessage {
        val message = AppMessage()
        val head = Head()
        head.msgId = msgId
        head.msgType = type
        head.msgContentType = subType
        head.fromId = fromId
        head.toId = toId
        head.extend = extend

        message.head = head
        message.body = content
        return message
    }

    /**
     * 根据聊天消息，生成一条可以能够传输通讯的消息
     *
     * @param msg
     * @return
     */
    fun buildAppMessage(msg: ContentMessage): AppMessage {
        val message = AppMessage()
        val head = Head()
        head.msgId = msg.msgId
        head.msgType = msg.msgType
        head.msgContentType = msg.msgContentType
        head.fromId = msg.fromId
        head.toId = msg.toId
        head.extend = msg.extend
        head.timestamp = msg.timestamp

        message.head = head
        message.body = msg.content
        return message
    }

    /**
     * 根据聊天消息，生成一条可以能够传输通讯的消息
     *
     * @param msg
     * @return
     */
    fun buildAppMessage(msg: BaseMessage): AppMessage {
        val message = AppMessage()
        val head = Head()

        head.msgId = msg.msgId
        head.msgType = msg.msgType
        head.msgContentType = msg.msgContentType
        head.fromId = msg.fromId
        head.toId = msg.toId
        head.extend = msg.extend
        head.timestamp = msg.timestamp

        message.head = head
        message.body = msg.content
        return message
    }

    /**
     * 根据业务消息对象获取protoBuf消息对应的builder
     *
     * @param message
     * @return
     */
    fun getProtoBufMessageBuilderByAppMessage(message: AppMessage): MessageProtobuf.Msg.Builder {
        val builder: MessageProtobuf.Msg.Builder = MessageProtobuf.Msg.newBuilder()

        val headBuilder: MessageProtobuf.Head.Builder = MessageProtobuf.Head.newBuilder()

        // 这里好 好好看 再赋值
        headBuilder.msgType = message.head?.msgType ?: 0
        headBuilder.statusReport = message.head?.statusReport ?: 0
        headBuilder.msgContentType = message.head?.msgContentType ?: 0


        if(!message.head?.msgId.isNullOrEmpty())
            headBuilder.msgId = message.head!!.msgId

        if(!message.head?.fromId.isNullOrEmpty())
            headBuilder.fromId = message.head!!.fromId

        if(!message.head?.toId.isNullOrEmpty())
            headBuilder.toId = message.head!!.toId

        if(message.head != null && message.head!!.timestamp != 0L)
            headBuilder.timestamp = message.head!!.timestamp

        if(!message.head?.extend.isNullOrEmpty())
            headBuilder.extend = message.head!!.extend

        if(!message.body.isNullOrEmpty())
            builder.body = message.body

        builder.setHead(headBuilder)

        return builder
    }

    /**
     * 通过protobuf消息对象获取业务消息对象
     *
     * @param protobufMessage
     * @return
     */
    fun getMessageByProtobuf(protobufMessage: MessageProtobuf.Msg): AppMessage {
        val message = AppMessage()
        val head = Head()
        val protoHead: MessageProtobuf.Head = protobufMessage.head

        head.msgId = protoHead.msgId
        head.msgType = protoHead.msgType
        head.statusReport = protoHead.statusReport
        head.msgContentType = protoHead.msgContentType
        head.fromId = protoHead.fromId
        head.toId = protoHead.toId
        head.extend = protoHead.extend
        head.timestamp = protoHead.timestamp

        message.head = head
        message.body = protobufMessage.body
        return message
    }
}